#include <stdio.h>
#include <stdlib.h>
#include "sqstring.cpp"

sqstring *maxsubstr(sqstring s) {
    sqstring *subs;
    int index = 0, length = 0, length1, i = 0, j, k;
    while (i < s.length) {
        j = i + 1;
        while (j < s.length) {
            if (s.data[i] == s.data[j]) {
                length1 = 1;
                for (k = 1; s.data[i + k] == s.data[j + k]; k++)
                    length1++;
                if (length1 > length) {
                    index = i;
                    length = length1;
                }
                j += length1;
            } else {
                j++;
            }
        }
        i++;
    }
    subs = (sqstring *) malloc(sizeof(sqstring));
    subs->length = length;
    for (i = 0; i < length; i++)
        subs->data[i] = s.data[index + i];
    return subs;
}

int main() {
    char str[maxsize];
    sqstring s, *subs;
    printf("输入串:");
    gets(str);
    strassign(s, str);
    subs = maxsubstr(s);
    printf("求最长重复字串:\n");
    printf("    原串");
    dispstr(s);
    printf("  最长重复字串:");
    dispstr(*subs);
    destroy(s);
    free(subs);
    return 0;
}
